//
//  RCOrderActivityManageDetailVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-10-2.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCOrderActivityManageDetailVC.h"
#import "IManager+RequestServerDetailInfo.h"
#import "AppDelegate.h"
#import "RCOrderDetailInfoCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "RCOderDetailModel.h"
#import "RCBaseServerDataModel.h"
#import "RCOrderManagerVC.h"
#import "RCServerNoticeVC.h"
#import "RCOrderUpdateStateVC.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"

#define kBottomBarImageFile       @"image_merchant_bottom_bar.png"


@interface RCOrderActivityManageDetailVC ()<IManagerDelegate>{
    DetailVCType _vcType;
    int64_t _reqID;
    
    UILabel *nameLabel;
    UILabel *timeLabel;
    UILabel *stateLabel;
    UILabel *sexLabel;
    UILabel *phoneLabel;
    UILabel *reserveTime; //预订时间
    UILabel *reserveLabel;
    UILabel *reserveCountLabel;
    UILabel *reserveType;
    UILabel *reserveTypeLabel;
    
    TTTAttributedLabel *leaveMsg;
    UILabel *spendLabel;
    UILabel *shopNote;
    UILabel *score;
    
}


@property (nonatomic, copy) NSString *uniqueID;
@property (nonatomic, copy) NSString *eventID;

@property (nonatomic, copy) NSString *orderUser;
@property (nonatomic, copy) NSString *orderPhone;

@property (nonatomic, copy) NSString *price;
@property (nonatomic, copy) NSString *remark;

@end

@implementation RCOrderActivityManageDetailVC
@synthesize uniqueID = _uniqueID;
@synthesize eventID = _eventID;
@synthesize orderPhone = _orderPhone;
@synthesize orderUser = _orderUser;

@synthesize state = _state;
@synthesize price = _price;
@synthesize remark = _remark;

- (id) initWithVCType:(DetailVCType)type withID:(NSString *)uniqueID withEventID:(NSString *)eventID
{
    self = [super init];
    if (self) {
        _vcType = type;
        self.uniqueID = uniqueID;
        self.eventID = eventID;
    }
    return self;
}


- (void) dealloc
{
    KF_RELEASE_SAFELY(_orderUser);
    KF_RELEASE_SAFELY(_orderPhone);
    KF_RELEASE_SAFELY(_uniqueID);
    
    KF_RELEASE_SAFELY(_price);
    KF_RELEASE_SAFELY(_remark);
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    // 设置tableview相关参数
//    if ( baseVCTableView != nil ) {
//        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
//        //        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
//        baseVCTableView.backgroundColor = [UIColor clearColor];
//        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        baseVCTableView.frame = self.view.bounds;
//        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20-self.navigationController.navigationBar.frame.size.height-4);
//        baseVCTableView.frame = tableViewNewFrame;
//        baseVCTableView.tableHeaderView = nil;
//        baseVCTableView.tableFooterView = nil;
////        [self addPullRefresh];
//    }
    if (_vcType == DetailvcType_Order) {
        self.titleLabelText = @"订单详情页";
    }else{
        self.titleLabelText = @"参与人数";
    }
    self.navigationItem.titleView = [self createTitleView];
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 230, 15)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.textColor = [UIColor colorWithHex:0xfffb8720];
    nameLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    [self.viewContainer addSubview:nameLabel];
    [nameLabel release];
    
    
    
    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 15, 110, 12)];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.textColor = [UIColor colorWithHex:0xffbababa];
    timeLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [self.viewContainer addSubview:timeLabel];
    [timeLabel release];
    
    
    stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 12)];
    stateLabel.backgroundColor = [UIColor clearColor];
    stateLabel.textColor = [UIColor colorWithHex:0xffbababa];
    stateLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    [self.viewContainer addSubview:stateLabel];
    [stateLabel release];
    
    sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 60, 15)];
    sexLabel.backgroundColor = [UIColor clearColor];
    sexLabel.textColor = [UIColor colorWithHex:0xff656565];
    sexLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:sexLabel];
    [sexLabel release];
    
    
    phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 160, 15)];
    phoneLabel.backgroundColor = [UIColor clearColor];
    phoneLabel.textColor = [UIColor colorWithHex:0xff656565];
    phoneLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    [self.viewContainer addSubview:phoneLabel];
    [phoneLabel release];
    
    //预订时间
    reserveTime = [[UILabel alloc] initWithFrame:CGRectMake(15, 75, 260, 15)];
    reserveTime.backgroundColor = [UIColor clearColor];
    reserveTime.textColor = [UIColor colorWithHex:0xff656565];
    reserveTime.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    [self.viewContainer addSubview:reserveTime];
    [reserveTime release];
    
    //预订人数
    reserveLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 95, 75, 15)];
    reserveLabel.backgroundColor = [UIColor clearColor];
    reserveLabel.textColor = [UIColor colorWithHex:0xff656565];
    reserveLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:reserveLabel];
    reserveLabel.text = @"预订人数：";
    [reserveLabel release];
    //预订人数
    reserveCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 95, 50, 15)];
    reserveCountLabel.backgroundColor = [UIColor clearColor];
    reserveCountLabel.textColor = [UIColor colorWithHex:0xfffb8720];
    reserveCountLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
    [self.viewContainer addSubview:reserveCountLabel];
    [reserveCountLabel release];
    
    
    //预订类型
    reserveType = [[UILabel alloc] initWithFrame:CGRectMake(110, 95, 65, 15)];
    reserveType.backgroundColor = [UIColor clearColor];
    reserveType.textColor = [UIColor colorWithHex:0xff656565];
    reserveType.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:reserveType];
    reserveType.text = @"预订类型:";
    [reserveType release];
    //预订类型
    reserveTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 95, 126, 15)];
    reserveTypeLabel.backgroundColor = [UIColor clearColor];
    reserveTypeLabel.textColor = [UIColor colorWithHex:0xfffb8720];
    reserveTypeLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:reserveTypeLabel];
    [reserveTypeLabel release];
    
    
    leaveMsg = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(15, 115, 280, 15)];
    leaveMsg.backgroundColor = [UIColor clearColor];
    leaveMsg.textColor = [UIColor colorWithHex:0xff656565];
    leaveMsg.numberOfLines = 0;
    leaveMsg.leading = 6;
    leaveMsg.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:leaveMsg];
    [leaveMsg release];
    
    
    spendLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 135, 280, 15)];
    spendLabel.backgroundColor = [UIColor clearColor];
    spendLabel.textColor = [UIColor colorWithHex:0xff656565];
    spendLabel.font = [UIFont systemFontOfSize:15];
    [self.viewContainer addSubview:spendLabel];
    [spendLabel release];
    
    
    shopNote = [[UILabel alloc] initWithFrame:CGRectMake(15, 155, 280, 15)];
    shopNote.backgroundColor = [UIColor clearColor];
    shopNote.textColor = [UIColor colorWithHex:0xff656565];
    shopNote.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:shopNote];
    [shopNote release];
    
    
    
    score = [[UILabel alloc] initWithFrame:CGRectMake(15, 175, 160, 15)];
    score.backgroundColor = [UIColor clearColor];
    score.textColor = [UIColor colorWithHex:0xff656565];
    score.font = [UIFont fontWithName:kFontNameLightHeiti size:15];
    [self.viewContainer addSubview:score];
    [score release];
    
    
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    
    UIImageView *bottomBGImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, UIScreenHeight-64-46, 320, 46)];
    bottomBGImageView.backgroundColor = [UIColor clearColor];
    NSString *bottomPath = [resourcePath stringByAppendingPathComponent:kBottomBarImageFile];
    [bottomBGImageView setImage:[UIImage imageWithContentsOfFile:bottomPath]];
    
    bottomBGImageView.userInteractionEnabled = YES;
    
    UIButton *signButton = [[UIButton alloc]initWithFrame:CGRectMake(22, 4, 22, 20)];
    signButton.backgroundColor = [UIColor clearColor];
    NSString *signPath = [resourcePath stringByAppendingPathComponent:@"button_orderDetail_01.png"];
    [signButton setImage:[UIImage imageWithContentsOfFile:signPath] forState:UIControlStateNormal];
    [signButton setImage:[UIImage imageWithContentsOfFile:signPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:signButton];
    [signButton release];
    
    UILabel *signLabel = [[UILabel alloc]initWithFrame:CGRectMake(13, 27, 50, 11)];
    signLabel.backgroundColor = [UIColor clearColor];
    signLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    signLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    signLabel.text = @"更新状态";
    [bottomBGImageView addSubview:signLabel];
    [signLabel release];
    
    UIButton *signTransButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 46)];
    signTransButton.backgroundColor = [UIColor clearColor];
    [signTransButton addTarget:self action:@selector(updateOrderState) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:signTransButton];
    [signTransButton release];
    
    
    UIButton *sendPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(152, 4, 22, 20)];
    sendPhotoButton.backgroundColor = [UIColor clearColor];
    NSString *sendPhotoPath = [resourcePath stringByAppendingPathComponent:@"button_orderDetail_02.png"];
    [sendPhotoButton setImage:[UIImage imageWithContentsOfFile:sendPhotoPath] forState:UIControlStateNormal];
    [sendPhotoButton setImage:[UIImage imageWithContentsOfFile:sendPhotoPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:sendPhotoButton];
    [sendPhotoButton release];
    
    UILabel *sendPhotoLabel = [[UILabel alloc]initWithFrame:CGRectMake(141, 27, 50, 11)];
    sendPhotoLabel.backgroundColor = [UIColor clearColor];
    sendPhotoLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    sendPhotoLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    sendPhotoLabel.text = @"通知客户";
    [bottomBGImageView addSubview:sendPhotoLabel];
    [sendPhotoLabel release];
    
    UIButton *sendPhotoTransButton = [[UIButton alloc]initWithFrame:CGRectMake(110, 0, 100, 46)];
    sendPhotoTransButton.backgroundColor = [UIColor clearColor];
    [sendPhotoTransButton addTarget:self action:@selector(noticeUser) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:sendPhotoTransButton];
    [sendPhotoTransButton release];
    
    
    
    UIButton *wrongButton = [[UIButton alloc]initWithFrame:CGRectMake(280, 4, 20, 20)];
    wrongButton.backgroundColor = [UIColor clearColor];
    NSString *wrongPath = [resourcePath stringByAppendingPathComponent:@"button_orderDetail_03.png"];
    [wrongButton setImage:[UIImage imageWithContentsOfFile:wrongPath] forState:UIControlStateNormal];
    [wrongButton setImage:[UIImage imageWithContentsOfFile:wrongPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:wrongButton];
    [wrongButton release];
    
    UILabel *wrongLabel = [[UILabel alloc]initWithFrame:CGRectMake(279, 27, 50, 11)];
    wrongLabel.backgroundColor = [UIColor clearColor];
    wrongLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    wrongLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    wrongLabel.text = @"删除";
    [bottomBGImageView addSubview:wrongLabel];
    [wrongLabel release];
    
    UIButton *wrongTransButton = [[UIButton alloc]initWithFrame:CGRectMake(220, 0, 100, 46)];
    wrongTransButton.backgroundColor = [UIColor clearColor];
    [wrongTransButton addTarget:self action:@selector(delOrder) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:wrongTransButton];
    [wrongTransButton release];
    
    [self.viewContainer addSubview:bottomBGImageView];
    [bottomBGImageView release];
    
    
    [self refreshData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 0;
//}
//
//- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    RCOrderDetailInfoCell *cellInfo = [[RCOrderDetailInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCOrderDetailInfoCell"];
//    
//    return cellInfo;
//}


- (void) refreshData
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    if (_vcType == RCListType_Order) {
        _reqID = [[IManager sharedInstance] requestOrderDetailInfo:self.uniqueID withIsOrder:YES withDelegate:self];
    }else{
        _reqID = [[IManager sharedInstance] requestOrderDetailInfo:self.uniqueID withIsOrder:NO withDelegate:self];

    }
}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"加载失败"];
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    
    if (dataModel.errorNO == RCErrorTypeNoError) {
        if ([dataModel isKindOfClass:[RCOderDetailModel class]]) {
            RCOderDetailModel *model = (RCOderDetailModel *)dataModel;
            nameLabel.text = [NSString stringWithFormat:@"姓名：%@", model.name];
            self.orderUser = model.name;
            stateLabel.text = model.status;
            
            timeLabel.text = model.datetime;
            sexLabel.text = [NSString stringWithFormat:@"性别：%@",model.sex];
            phoneLabel.text = [NSString stringWithFormat:@"电话：%@",model.phone];
            self.orderPhone = model.phone;
            reserveTime.text =[NSString stringWithFormat:@"预订时间：%@",model.reversetime];
            reserveCountLabel.text = model.persons;
            reserveTypeLabel.text = model.type;
            
            leaveMsg.text = [NSString stringWithFormat:@"客户留言：%@",model.content];
            CGSize size = [leaveMsg.attributedText sizeWithConstrainedToSize:CGSizeMake(2000, 280) Leading:6];
            CGRect rect = leaveMsg.frame;
            rect.size.width = size.width;
            leaveMsg.frame = rect;
            
            spendLabel.text =[NSString stringWithFormat:@"消费金额：%@",model.price];
            self.price = model.price;
            rect = spendLabel.frame;
            rect.origin.y = CGRectGetMaxY(leaveMsg.frame)+20;
            spendLabel.frame = rect;
            
            shopNote.text = [NSString stringWithFormat:@"商家备注：%@",model.remark];
            rect = shopNote.frame;
            rect.origin.y = CGRectGetMaxY(spendLabel.frame)+5;
            shopNote.frame = rect;
            self.remark = model.remark;
            
            score.text = [NSString stringWithFormat:@"赠送积分：%@",model.awardpoint];
            rect = score.frame;
            rect.origin.y = CGRectGetMaxY(shopNote.frame)+5;
            score.frame = rect;
            
        }else if ([dataModel isKindOfClass:[RCBaseServerDataModel class]]){
            NSArray *t = [self.navigationController viewControllers];
            RCOrderManagerVC *vc = [t objectAtIndex:([t count] -2)];
            [vc refreshData];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
       
        
    }else{
        [RCAppDelegate alertWithCustomizeString:[NSString stringWithFormat:@"%@",dataModel.errorMsg]];
    }
}


#pragma mark
- (void) updateOrderState
{
    RCOrderUpdateStateVC *vc = nil;
    if (_vcType == DetailvcType_Order) {
        vc =   [[RCOrderUpdateStateVC alloc] initWithUniqueID:self.uniqueID withEventID:nil withIsOrder:YES withState:self.state withPrice:self.price withNote:self.remark];
    }else{
       vc = [[RCOrderUpdateStateVC alloc] initWithUniqueID:self.uniqueID withEventID:self.eventID withIsOrder:NO withState:self.state withPrice:self.price withNote:self.remark];

    }
//    RCOrderUpdateStateVC *vc = [[RCOrderUpdateStateVC alloc] initWithUniqueID:self.uniqueID withState:self.state withPrice:self.price withNote:self.remark];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


- (void) noticeUser
{
    RCServerNoticeVC *vc = nil;
     if (_vcType == DetailvcType_Order) {
         vc  = [[RCServerNoticeVC alloc] initWithUserName:self.orderUser withPhone:self.orderPhone withOrderID:self.uniqueID  withIsOrder:YES];
     }else{
         vc  = [[RCServerNoticeVC alloc] initWithUserName:self.orderUser withPhone:self.orderPhone withOrderID:self.uniqueID  withIsOrder:NO];
         
     }
    
        
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
    
}

- (void) delOrder
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    if (_vcType == DetailvcType_Order) {
        _reqID =  [[IManager sharedInstance] requestDelOrderDetailInfo:self.uniqueID withDelegate:self withIsOrder:YES];
    }else{
        _reqID = [[IManager sharedInstance] requestDelOrderDetailInfo:self.uniqueID withDelegate:self withIsOrder:NO];
    }
}
@end

